#ifndef XX_COMMON_HTTPSESSION_HH
#define XX_COMMON_HTTPSESSION_HH
#include "httpParser.h"
#include <thread>
#include <functional>
#include "ThreadPoolWork.h"
#include "JSONParse.h"
#include "Buffer.h"
#include "IModel.h"

class CHTTPSession : public CHttpParser, public zl::Task
{
public:
	// using FunctionPtr = void (CHTTPSession::*)(result* res);
	// struct ApiEntry {
	// 	FunctionPtr callback;
	// 	std::string method;
	// 	std::string operation;
	// 	std::string operate_des;
	// };

	// using ApiMap = std::unordered_map<std::string, ApiEntry>;
	std::unordered_map<std::string,IModel*> m_funModel;
	CHTTPSession();
	virtual int run();
	virtual ~CHTTPSession();
	virtual int CheckActive();
	bool CheckToken(std::string& Authorization);
	virtual int StopServer();
	virtual int StartServer(const socket_t fdClient);
	virtual void OnReceive(const char* data, int len);
	virtual int sendMessage(const char* data, int len);
	void setClientIp(const char* str);

	virtual int    sendFiles(const char* FilePath);
	void sendJson(cJSON* json);
	void sendSessionMessage(const char* data);

private:
	void device_list(result* res);
	void device_channellist(result* res);

private:
	virtual void   send200Message();
	virtual void   send400Message(const char* data, int len);
	virtual void   send404Message(const char* data, int len);
	virtual void   send401Message(const char* data, int len);
	virtual void   send302Message(const char* data);
	virtual void   sendJsonMessage(const char* data, int len);
	virtual void   sendTextMessage(const char* data, int len);
	virtual void   sendBodyMessage(const char* data, int len);

	void initApiMap1();
	void initApiMap2();
	void initApiMap4();
	void initApiMapGet();

	int httpMessage();
	int httpMessage1(CData& ApiStr);
	int httpMessage2(CData& ApiStr);
	int httpMessage3(CData& ApiStr);
	int httpMessage4(CData& ApiStr);
	int httpMessageGet();
	int httpMessageGetFile();
	int httpMessageGetApi(CData& ApiStr);
	int httpMessagePost();
	int httpMessageOPTIONS();
private:
	// ApiMap apiMap1;
	// ApiMap apiMap2;
	// ApiMap apiMap4;
	// ApiMap apiMapGet;
	// char*			m_chars;
	// unsigned int	m_lengh;
	unsigned int	m_RecvBye;
	socket_t		m_fdClient;
	std::string 	m_strClientIp;
	CHttpBuffer* m_HttpBuffer;
	std::string		m_strUacId;
	std::string		m_strMainServiceId;

	std::string		m_strLogUser;
	std::string		g_start_time;
	std::string		g_end_time;

	Buffer rx_buf;

	std::string m_errorMsg;
public:
	bool 			m_isSendFile;
};
#endif
